package com.vm.fragment;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import com.vm.activity.SearchActivity;
import com.vm.adapter.FragAdapter;
import com.vm.myview.NoScrollViewPager;
import com.wwtscapp.apk.R;

@SuppressLint("InflateParams")
public class IndexFragment extends BaseFragment {

	private View view;
	RadioButton rbtn_found;
	/*Button rbtn_app;*/
	RadioButton rbtn_new;
	Button rbtn_game;
	private NoScrollViewPager vp;
	/** 标志位，标志已经初始化完成 */
	private boolean isPrepared;
	/** 是否已被加载过一次，第二次就不再去请求数据了 */
	private boolean mHasLoadedOnce;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i("CategoryFragment", "onCreateView");

		if (view == null) {
			view = inflater.inflate(R.layout.index, null);
		} else {
			// 缓存的rootView需要判断是否已经被加过parent
			// 如果有parent需要从parent删除
			ViewGroup parent = (ViewGroup) view.getParent();
			if (parent != null) {
				parent.removeView(view);
			}
		}
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		isPrepared = true;
		lazyLoad();

	}

	/**
	 * 初始化控件
	 */
	public void init() {
		rbtn_found = (RadioButton) view.findViewById(R.id.rbtn_found);
		/*rbtn_app = (Button) view.findViewById(R.id.rbtn_app);*/
		rbtn_new = (RadioButton) view.findViewById(R.id.rbtn_new);
		/*rbtn_game = (Button) view.findViewById(R.id.rbtn_game);*/

		rbtn_found.setOnClickListener(btn_listener);
		/*rbtn_app.setOnClickListener(btn_listener);*/
		rbtn_new.setOnClickListener(btn_listener);
		/*rbtn_game.setOnClickListener(btn_listener);*/

	}

	OnClickListener btn_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
//			Intent intent;
			switch (v.getId()) {
			case R.id.rbtn_found:
				vp.setCurrentItem(0);
				break;
			/*case R.id.rbtn_app:
				vp.setCurrentItem(1);

				break;*/
			case R.id.rbtn_new:

				break;
			/*case R.id.rbtn_game:
				vp.setCurrentItem(2);
				break;
*/
			default:
				break;
			}
		}
	};

	public OnClickListener searchClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(getActivity(), SearchActivity.class);
			startActivity(intent);
		}
	};

	@Override
	protected void lazyLoad() {
		// TODO Auto-generated method stub
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}

		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				// 显示加载进度对话框
				// UIHelper.showDialogForLoading(getActivity(), "正在加载...",
				// true);
				mainHandler.sendMessage(new Message());
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				try {
					Thread.sleep(300);

					// 在这里添加调用接口获取数据的代码
					// doSomething()
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}

			@Override
			protected void onPostExecute(Boolean isSuccess) {
				if (isSuccess) {
					// 加载成功

					mHasLoadedOnce = true;
				} else {
					// 加载失败
				}
				// 关闭对话框
				// UIHelper.hideDialogForLoading();
			}
		}.execute();
	}

	private void iniChilds() {
		List<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(new FoundFragment());
		fragments.add(new NecessaryAppFragment());
		fragments.add(new NecessaryGameFragment());

		// fragments.add(new CategoryGameFragment());
		vp = (NoScrollViewPager) view.findViewById(R.id.viewpager);
		FragAdapter adapter = new FragAdapter(getChildFragmentManager(),
				fragments);
		vp.setAdapter(adapter);

		// 设置最大缓存数
		// 默认缓存2个页面,当出现第三个页面会删掉之前缓存中的一个.
		vp.setOffscreenPageLimit(3);
		vp.setCurrentItem(0);
	}

	private Handler mainHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			init();
			iniChilds();

		};
	};

}
